﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Util;

namespace DAO
{
    public class User
    {
        DBConnection DBConnection = new DBConnection();
        public User()
        {
        }

        /// <summary>
        /// Get all DTO.User instance from DB.
        /// </summary>
        /// <returns>List<> of all DTO.User instances.</returns>
        public List<DTO.User> GetAllUsers()
        {
            Log.LogInfo("Begin GetAllUsers()");
            String query = "SELECT * FROM [User];";
            DBConnection DB = new DBConnection();
            SqlDataReader reader = DB.ExecuteReader(query);
            List<DTO.User> userDTOs = new List<DTO.User>();
            try
            {
                while (reader.Read())
                {
                    userDTOs.Add(new DTO.User(reader));
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex.StackTrace);
            }

            DB.Close();
            Log.LogInfo("End GetAllUsers()");
            DBConnection.Close();
            return userDTOs;
        }

        /// <summary>
        /// Get DTO.User instance from DB.
        /// </summary>
        /// <param name="ID">User ID</param>
        /// <returns>DTO.User instance which has the specific ID.</returns>
        public DTO.User GetUser(String ID)
        {
            Log.LogInfo("Begin GetUser(String ID)");
            String query = "SELECT * FROM [User] WHERE [User].[ID]='" + ID + "';";
            DBConnection DB = new DBConnection();
            SqlDataReader reader = DB.ExecuteReader(query);

            DTO.User userDTO = null;
            try
            {
                if (reader.Read())
                    userDTO = new DTO.User(reader);
            }
            catch (Exception ex)
            {
                Log.LogError(ex.StackTrace);
            }

            DB.Close();
            Log.LogInfo("End GetUser(String ID)");
            return userDTO;
        }

        /// <summary>
        /// Insert User into database.
        /// </summary>
        /// <param name="User">DTO.User instance to insert into database.</param>
        public int InsertUser(DTO.User User)
        {
            Log.LogInfo("Begin InsertUser()");
            //int i=0;
            //using (DataClassesBlockProviderDataContext dataContextBlog = new DataClassesBlockProviderDataContext())
            //{
            //    i= dataContextBlog.udsUser_Insert(User.ID, User.Name, User.Gender, User.Birth, User.AvatarPath, User.BlogName, User.BlogDescription, User.CoverImagePath, User.ThemeID, Byte.Parse(User.LayoutID.ToString()), User.Question1, User.Answer1, User.Question2, User.Answer2, User.PassWord);
            //};
            String query = "INSERT INTO [USER] (ID, Name, Gender, Birth, Avatar, BlogName, BlogDescription, CoverImage, Question1, Answer1, Question2, Answer2) VALUES ('";
            query += User.ID + "','" + User.Name + "','" + User.Gender + "','" + User.Birth + "','" + User.AvatarPath + "','" + User.BlogName + "','" + User.BlogDescription + "','" + User.CoverImagePath + "','" + "','" + User.Question1 + "','" + User.Answer1 + "','" + User.Question2 + "','" + User.Answer2 + "')";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Insert " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertUser()");
            return result;
        }

        /// <summary>
        /// Update User info into database.
        /// </summary>
        /// <param name="User">User instance to be updated.</param>
        public void UpdateUser(DTO.User User)
        {
            Log.LogInfo("Begin UpdateUser()");
            String query = "UPDATE [USER] SET Name = '" + User.Name + "','" + " Gender = '" + User.Gender + "','" + "Birth = " + User.Birth + "','" + "Avatar = '" + User.AvatarPath + "','" + "BlogName = '" + User.BlogName + "','" + "BlogDescription = '" + User.BlogName + "','" + "CoverImage = '" + User.CoverImagePath + "','" + "','" + "Question1 = '" + User.Question1 + "','" + "Answer1 = '" + User.Answer1 + "','" + "Question2 = '" + User.Question2 + "','" + "Answer2 = '" + User.Answer2 + " ' WHERE ID = " + User.ID + "'";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Update " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertUser()");
        }
        //UpdateBlogInfo
        public int UpdateBlogInfo(DTO.User User)
        {
            Log.LogInfo("Begin UpdateBlogInfo()");
            String query = "UPDATE [USER] SET BlogName = N'" + User.BlogName+ "'," + " BlogDescription = N'" + User.BlogDescription+"' where ID='"+User.ID+"';" ;
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Update " + result.ToString() + "row(s).");
            Log.LogInfo("End UpdateBlogInfo()");
            return result;
        }
        /// <summary>
        /// Delete a specific User from DB.
        /// </summary>
        /// <param name="User">The User to be deleted.</param>
        public void DeleteUser(DTO.User User)
        {
            Log.LogInfo("Begin DeleteUser()");
            String query = "DELETE FROM [User] WHERE [User].[ID]='" + User.ID + "';";
            DBConnection DB = new DBConnection();
            int result = DB.ExecuteNonQuery(query);
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            DB.Close();
            Log.LogInfo("End DeleteUser()");
        }

        /// <summary>
        /// Delete a User in database using ID.
        /// </summary>
        /// <param name="ID">User ID.</param>
        public void DeleteUser(String ID)
        {
            Log.LogInfo("Begin DeleteUser()");
            String query = "DELETE FROM [User] WHERE [User].[ID]='" + ID + "';";
            DBConnection DB = new DBConnection();
            int result = DB.ExecuteNonQuery(query);
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            DB.Close();
            Log.LogInfo("End DeleteUser()");
        }

        internal DTO.User GetUser(string Id, string pass)
        {
            Log.LogInfo("Begin GetUser(String ID,string pass)");
            String query = "SELECT * FROM [User] WHERE [User].[ID]='" + Id + "' AND [User].[Password]='"+pass+"';";
            DBConnection DB = new DBConnection();
            SqlDataReader reader = DB.ExecuteReader(query);
            DTO.User userDTO = null;
            try
            {
                if (reader.Read())
                    userDTO = new DTO.User(reader);
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetUser(String ID): " + ex.Message);
            }
            DB.Close();
            Log.LogInfo("End GetUser(String ID,string pass)");
            return userDTO;
        }
    }
}